<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/8/13
 * Time: 15:33
 */

namespace app\admin\model;


use think\Model;

class Mechanism extends Model
{
    /**
     * 获取所有机构名称和ID
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getMechanismInfo(){
        $list = $this->field('mechanism_id, mechanism_name, mechanism_type, mechanism_pid_id')
            ->where('mechanism_status', 2)
            ->where('mechanism_id', 'neq', 1)
            ->select();
        return $list;
    }
    
    /**
     * 获取机构代码
     * @param $mechanism_id
     * @return mixed
     */
    public function getMechanismCode($mechanism_id){
        $code = $this->field('mechanism_code, mechanism_type')->where('mechanism_id', $mechanism_id)->find();
        return $code;
    }
    
    /**
     * 获取所有已认证、没有下级机构的除总队以外的机构
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getNoSonMechanism(){
        $lists = $this->field('mechanism_id, mechanism_code, mechanism_name')
            ->where('mechanism_status', 2)
            ->where('mechanism_id', 'neq', 1)
            ->where('next_mechanism', 1)
            ->select();
        return $lists->toArray();
    }
    
    /**
     * 获取所有已认证、非5级、去除总队外的所有机构
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getExceptFive(){
        $exceptFive = $this->field('mechanism_id, mechanism_code, mechanism_name, mechanism_type')
            ->where('mechanism_status', 2)//已认证
            ->where('mechanism_id', 'neq', 1)//不是总队
            ->where('mechanism_type', 'neq', 5)//除去5级机构
            ->select();
        return $exceptFive;
    }
    
    /**
     * 查询所有机构
     * @param $where
     * @param $field
     * @param $page
     * @param $limt
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getMechanismLists($where, $field, $page, $limt){
        $mechanismLists = $this->alias('um')
            ->join('up_area ua1', 'ua1.district_id = um.mechanism_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = um.mechanism_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = um.mechanism_qu', 'left')
            ->join('up_mechanism um1', 'um1.mechanism_id = um.mechanism_pid_id', 'left')
            ->where('um.mechanism_id', 'neq', 1)
            ->where($where)
            ->field($field)
            ->page($page, $limt)
            ->order('um.mechanism_id desc')
            ->select();
        return $mechanismLists;
    }
    
    /**
     * 获取条件搜索的记录总数，用于分页
     * @param $where  搜索条件
     * @return int|string
     */
    public function getCount($where){
        $count = $this->alias('um')
            ->join('up_area ua1', 'ua1.district_id = um.mechanism_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = um.mechanism_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = um.mechanism_qu', 'left')
            ->join('up_mechanism um1', 'um1.mechanism_id = um.mechanism_pid_id', 'left')
            ->where('um.mechanism_id', 'neq', 1)
            ->where($where)
            ->count();
        return $count;
    }
    
    /**
     * 判断机构代码是否重复
     * @param $mechanism_code
     * @return $this
     */
    public function codeRe($mechanism_code){
        $res = $this->where('mechanism_code', $mechanism_code)->find();
        return $res;
    }
    
    /**
     * 新建一级机构
     * @param $data
     * @return false|int
     */
    public function insertMechanism($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    
    /**
     * 根据机构ID，获取相关字段
     * @param $mechanism_id
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     */
    public function getInfo($mechanism_id, $field){
        $info = $this->field($field)->where('mechanism_id', $mechanism_id)->find();
        return $info;
    }
    
    /**
     * 更新机构信息
     * @param $mechanism_id
     * @param $data
     * @return $this
     */
    public function updateInfo($mechanism_id, $data){
        $res = $this->where('mechanism_id', $mechanism_id)->update($data);
        return $res;
    }
    
    /**
     * 审核机构
     * @param $mechanism_id
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     */
    public function mechanismAudit($mechanism_id, $field){
        $info = $this->alias('um')
            ->join('up_mechanism_member umm', 'umm.id = um.porposer_id', 'left')
            ->join('up_mechanism um1', 'um1.mechanism_id = um.mechanism_pid_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = um.mechanism_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = um.mechanism_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = um.mechanism_qu', 'left')
            ->where('um.mechanism_id', $mechanism_id)
            ->field($field)
            ->find();
        return $info;
    }
    
    /**
     * 查询单个机构信息
     * @param $where
     * @param $field
     * @param $page
     * @param $limt
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOneInfo($where, $field){
        $oneInfo = $this
//            ->join('up_area ua1', 'ua1.district_id = um.mechanism_sheng', 'left')
//            ->join('up_area ua2', 'ua2.district_id = um.mechanism_shi', 'left')
//            ->join('up_area ua3', 'ua3.district_id = um.mechanism_qu', 'left')
//            ->join('up_mechanism um1', 'um1.mechanism_id = um.mechanism_pid_id', 'left')
//            ->where('mechanism_id', 'neq', 1)
            ->where($where)
            ->field($field)
            ->find();
        return $oneInfo;
    }
}